package bd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import logic.partida;

import bd.JDBC;

public class JDBCpartida implements JDBC{

	private static final String DRIVER_CLASS_NAME = "org.sqlite.JDBC";
	private static final String CONNECTION_URL = "jdbc:sqlite:DB/reversi.s3db";
	
	private Connection connection;
	private ResultSet resulset;
	
	public JDBCpartida(){
		try {
			Class.forName(DRIVER_CLASS_NAME);
			connection= DriverManager.getConnection(CONNECTION_URL);
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public void close() throws SQLException{
		connection.close();
	}
	
	@Override
	public void insert(Object o) throws SQLException, ClassNotFoundException {
		if(o instanceof partida){
			
			String sql="";
			partida v=(partida)o;
			sql= "INSERT INTO partida VALUES(?,?,?,?,?,?);";
			
			PreparedStatement pstmt = connection.prepareStatement( sql );
			
			pstmt.setString( 1, v.getJ1()); 
			pstmt.setString( 2, v.getJ2()); 
			pstmt.setString( 3, v.converTableroToString()); 
			pstmt.setString( 4, v.getDate());
			pstmt.setLong( 5, v.getTurno());
			pstmt.setLong( 6, v.getNumeroTurno());
			
			pstmt.execute( );
			
			pstmt.close();
		}else 
			throw new IllegalArgumentException("El parametro recibido debe ser de tipo User!");
	}

	@Override
	public void delete(Object o) throws SQLException {
		if(o instanceof partida){
			String sql="";
			partida v = (partida)o;
			sql= "DELETE FROM partida WHERE date=? and j1=?  and j2=?;";
			
			PreparedStatement pstmt = connection.prepareStatement( sql );
			pstmt.setString( 1, v.getDate()); 
			pstmt.setString( 2, v.getJ1()); 
			pstmt.setString( 3, v.getJ2()); 
			pstmt.execute( );			
			pstmt.close();			
		}else
			throw new IllegalArgumentException("El parametro recibido debe ser de tipo User!");
	}

	@Override
	public ArrayList<partida> selectAll() throws SQLException {
		Statement statement = null;
		statement =  connection.createStatement();
		String sql="";
		sql= "Select * FROM partida;";
		
		resulset=statement.executeQuery(sql);
		
		ArrayList<partida> lista= new ArrayList<partida>();
		
		
		while(resulset.next()){			
			lista.add(convertPartida(resulset));
		}
		statement.close();
		return lista;
	}

	@Override
	public partida selectOne(Object o) throws SQLException, IllegalArgumentException{		
		if(o instanceof partida){
			partida user = null;
			partida clave = (partida) o;
			String sql="";
			sql = "Select * FROM partida WHERE j1=? and j2=? and date=?;";			
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1,clave.getJ1());
			pstmt.setString(2,clave.getJ2());
			pstmt.setString(3,clave.getDate()); 
			resulset=pstmt.executeQuery(sql);			
			if(resulset.next()){			
				user = convertPartida(resulset);
			}
			pstmt.close();
			return user;
		}
		else throw new IllegalArgumentException("El parametro recibido debe ser de tipo String!");
	}

	@Override
	public void update(Object o) throws SQLException, ClassNotFoundException{
		delete(o);
		insert(o);		
	}
	
	/**
	 * Metodo que crea a partir de un resultSet un objetoUsuario
	 * @param res ResultSet a partir del cual crear un usuario
	 * @return Usuario creado
	 * @throws SQLException
	 */
	private partida convertPartida(ResultSet res) throws SQLException{
		partida p=new partida();
		
		p.setDate(res.getString("date"));
		
		p.setJ1(res.getString("j1"));
		p.setJ2(res.getString("j2"));
		p.setTurno(res.getInt("turno"));
		p.setNumeroTurno(res.getInt("numeroTurno"));
		p.converTableroToArray(res.getString("tablero"));
		return p;
	}

	public static void main (String[]args){
		
		partida p=new partida("j1", "j3", new int[8][8], "date", 0, 0);
		JDBCpartida j=new JDBCpartida();
		try {
			j.insert(p);
			System.out.println("everyting ok");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}


}

